package com.gradle.quartz.batch.service.impl;

import com.gradle.bean.employee.Employee;
import com.gradle.mapper.EmployeeMapper;
import com.gradle.quartz.batch.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.List;
import java.util.Random;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Value("${job.data.path}")
    private String filePath;

    @Override
    public void save(Employee employee) {
        employeeMapper.save(employee);
        System.out.println("执行EmployeeServiceImpl.save方法完成");
    }

    @Override
    public void saveTemp(Employee employee) {
        employeeMapper.saveTemp(employee);
        System.out.println("执行EmployeeServiceImpl.save方法完成");
    }

    @Override
    public List<Employee> queryEmployeeInfo() {
        return employeeMapper.queryEmployeeInfo();
    }

    @Override
    public void dataInit() {
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }
        try {
            boolean newFile = file.createNewFile();
            if (newFile) {
                System.out.println("文件创建成功");
            }
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            Random ageRandom = new Random();
            for (int i = 1; i <= 500000; i++) {
                if (i == 500000) {
                    String buffer = i + "," + "xiao_" + i + "," + ageRandom.nextInt(90) + "," + "广东省深圳市龙华区";
                    writer.write(buffer);
                } else {
                    String buffer = i + "," + "xiao_" + i + "," + ageRandom.nextInt(90) + "," + "广东省深圳市龙华区";
                    writer.write(buffer);
                    writer.newLine();
                }
            }
            writer.flush();
            writer.close();
            System.out.println("数据生成完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void truncateEmployee() {
        employeeMapper.truncateEmployee();
    }

    @Override
    public void truncateEmployeeTemp() {
        employeeMapper.truncateEmployeeTemp();
    }

    @Override
    public int countEmployeeTemp() {
        return employeeMapper.countEmployeeTemp();
    }
}
